package com.jrx.anytxn.param.controller.limit;

import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.limit.CreditLimitMappingReq;
import com.jrx.anytxn.param.dto.limit.CreditLimitMappingRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.limit.ICreditLimitMappingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname CreditLimitMappingController
 * @Description 额度节点映射表管理
 * @Date 2019/7/17 0017 下午 14:59
 * @author  by yf
 */
@Api(tags = "与页面相关的额度节点映射表参数API接口")
@RestController
@RequestMapping("/param")
public class CreditLimitMappingController {
    private static final Logger logger= LoggerFactory.getLogger(CreditLimitMappingController.class);

    @Autowired
    private InfoService infoService;

    @Autowired
    private ICreditLimitMappingService iCreditLimitMappingService;

    @ApiOperation(value="根据租户ID分页查询额度节点映射表信息",notes = "根据租户ID分页查询额度节点映射表信息")
    @GetMapping("/limitMappings/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="页大小",required = true)})
    public TxnRespResult<TxnPage<CreditLimitMappingRes>> getAllLimitMappingByPage(@PathVariable(value="pageNum") Integer pageNum,@PathVariable(value="pageSize") Integer pageSize){
        TxnRespResult<TxnPage<CreditLimitMappingRes>> result=new TxnRespResult<>();
        TxnPage<CreditLimitMappingRes> response;
        try {
            String tenantId= String.valueOf(infoService.getUser().getProjectId());
            response=iCreditLimitMappingService.findAllPageByTenId(pageNum,pageSize,tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(),e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID，渠道和产品ID查询额度节点映射表信息",notes = "根据租户ID，渠道和产品ID查询额度节点映射表信息")
    @GetMapping(value = "/limitMappings/{tenantId}/{channel}/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tenantId",value="租户ID",required = true),
            @ApiImplicitParam(name="channel",value="渠道号",required = true),
            @ApiImplicitParam(name="productId",value="产品ID",required = true)})
    public TxnRespResult<CreditLimitMappingRes> getLimitMappingById(@PathVariable String tenantId,@PathVariable String channel,@PathVariable String productId) {
        TxnRespResult<CreditLimitMappingRes> result = new TxnRespResult<>();
        CreditLimitMappingRes res;
        try {
            res = iCreditLimitMappingService.findByTenIdAndChannelAndProId(String.valueOf(infoService.getUser().getProjectId()), channel, productId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID，渠道和产品ID删除额度节点映射表信息",notes = "根据租户ID，渠道和产品ID删除额度节点映射表信息")
    @DeleteMapping(value = "/limitMappings/{tenantId}/{channel}/{productId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="tenantId",value="租户ID",required = true),
            @ApiImplicitParam(name="channel",value="渠道号",required = true),
            @ApiImplicitParam(name="productId",value="产品ID",required = true)})
    public TxnRespResult<Boolean> removeLimitMappingById(@PathVariable String tenantId,@PathVariable String channel,@PathVariable String productId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iCreditLimitMappingService.removeByTenIdAndChannelAndProId(String.valueOf(infoService.getUser().getProjectId()), channel, productId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "新增额度节点映射表信息",notes = "新增额度节点映射表信息")
    @PostMapping("/limitMappings")
    public TxnRespResult<CreditLimitMappingRes> createLimitMapping(@RequestBody CreditLimitMappingReq creditLimitMappingReq) {
        TxnRespResult<CreditLimitMappingRes> result = new TxnRespResult<>();
        CreditLimitMappingRes response;
        try {
            creditLimitMappingReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            creditLimitMappingReq.setUpdateBy(infoService.getUser().getUserName());
            response=iCreditLimitMappingService.addLimitMapping(creditLimitMappingReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(),e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑额度节点映射表信息",notes = "编辑额度节点映射表信息")
    @PutMapping("/limitMappings")
    public TxnRespResult<CreditLimitMappingRes> modifyLimitMapping(@RequestBody CreditLimitMappingReq creditLimitMappingReq) {
        TxnRespResult<CreditLimitMappingRes> result = new TxnRespResult<>();
        CreditLimitMappingRes response;
        creditLimitMappingReq.setUpdateBy(infoService.getUser().getUserName());
        response = iCreditLimitMappingService.modifyLimitMapping(creditLimitMappingReq);
        result.getSuccess(response);
        return result;
    }

}
